//
//  VVYCheckStatus.m
//  viviyoo
//
//  Created by viviyoo on 15/11/16.
//  Copyright © 2015年 viviyoo. All rights reserved.
//

#import "VVYCheckStatus.h"
#import "LYUserInfoModel.h"
#import "LYNavigationController.h"

@interface VVYCheckStatus ()

@property (nonatomic, assign, getter=isLogin, readwrite) BOOL login;


@end

@implementation VVYCheckStatus

static id _instance;

+ (instancetype)allocWithZone:(struct _NSZone *)zone
{
    @synchronized(self) {
        if (_instance == nil) {
            _instance = [super allocWithZone:zone];
        }
    }
    return _instance;
}

+ (instancetype)sharedInstance
{
    @synchronized(self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }
    return _instance;
}

- (id)copyWithZone:(NSZone *)zone
{
    return _instance;
}

- (BOOL)checkIsLogin{

    LYUserInfoModel * user_info = [VVYSaveDataManager getDataWithKey:KEY_USERMESSAGE];
    if (user_info != nil) {
        self.login = YES;
        return YES;
    }else{
        self.login = NO;
        return NO;
    }
}

- (BOOL)checkIsLoginBlock:(loginSuccessBlock)block{

    LYUserInfoModel * user_info = [VVYSaveDataManager getDataWithKey:KEY_USERMESSAGE];
    if (user_info != nil) {
        if (block) {
            block(user_info);
        }
        
        return YES;
    }else{
        //弹出登录
        LYLoginViewController *loginVC = [[LYLoginViewController alloc] init];
        loginVC.block = block;
        
        LYNavigationController *nav = [[LYNavigationController alloc] initWithRootViewController:loginVC];
        
        UIViewController *rootvc = [UIApplication sharedApplication].keyWindow.rootViewController;
        
        [rootvc presentViewController:nav animated:YES completion:nil];
        return NO;
    }
}

- (void)setLogin:(BOOL)login{
    
    _login = login;
    
    if (!_login) {
        [VVYSaveDataManager clearUserMessage];
        [[LYDataSourceManager sharedManager] resetUserInfo];
    }
    
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:LYLoginNotification object:nil];
}

- (void)loginOut{

    self.login = NO;
}
@end
